package com.qhit.controller;

import com.qhit.pojo.Goods;
import com.qhit.pojo.Order;
import com.qhit.pojo.Tourist;
import com.qhit.service.GoodsService;
import com.qhit.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("/list")
    public String list(Model m,String keywords,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer page,
                       @RequestParam(defaultValue = "1",required = false)
                       Integer pageSize){
        HashMap hashMap = new HashMap<>();
        hashMap.put("keywords",keywords);
        hashMap.put("start",(page-1)*pageSize);
        hashMap.put("pageSize",pageSize);
        hashMap.put("page",page);
        List<Map> orderList=orderService.getList(hashMap);
        int total= orderService.getListCount(hashMap);
        hashMap.put("totalPage",(total%pageSize==0)?(total/pageSize):(total/pageSize+1));
        m.addAttribute("orderList",orderList);
        m.addAttribute("myParams",hashMap);
        return "order/list";
    }
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("goods",goodsService.getList(null));
        return "order/add";
    }
    @RequestMapping(value = "/add",method =RequestMethod.POST)
    public String add(Order order, Model m){

        order.setOrderDate(new Date());
        order.setOrderMoney(0);
        order.setState(0);
       int r= this.orderService.add(order);
        if (r>0){
            m.addAttribute("msg","添加成功！！");
        }else {
            m.addAttribute("msg","添加失败！！");
        }
        return "order/msg";
    }
    @RequestMapping("/toEdit")
    public String toEdit(int id,Model model){
        Order g=orderService.getOrderById(id);
        model.addAttribute("goods",goodsService.getList(null));
        model.addAttribute("order",g);
        return "order/edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Order g,Model model){

        int re=orderService.edit(g);
        if (re>0){
            model.addAttribute("msg","修改成功！！");
        }else {
            model.addAttribute("msg","修改失败！！");
        }
        return "order/msg";
    }
    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public String del(int id,Model model){

        int re=orderService.del(id);
        if (re>0){
            model.addAttribute("msg","删除成功！！");
        }else {
            model.addAttribute("msg","删除失败！！");
        }
        return "order/msg";
    }

    @RequestMapping(value = "/editTourist",method = RequestMethod.GET)
    public String editTourist(int id,Model model){

        Order g=orderService.getOrderById(id);
        model.addAttribute("order",g);

        return "order/tourist_list";
    }
    @RequestMapping(value = "/getTouristData",method = RequestMethod.POST)
    public @ResponseBody
      List<Tourist> getTouristData(int id, Model model){
       List<Tourist> list= orderService.getTouristListByOrderId(id);
        return list;
    }

    @RequestMapping(value = "/getOutTouristData",method = RequestMethod.POST)
    public @ResponseBody
    List<Tourist> getOutTouristData(int id, Model model){
        List<Tourist> list= orderService.getOutTouristData(id);
        return list;
    }



    @RequestMapping(value = "/addTourist",method = RequestMethod.POST)
    public @ResponseBody String  addTourist(int order_id,String tourist_id, Model model){
        int list= orderService.addTourist(order_id,tourist_id);
        if(list>0){
            return "success";
        }else {
            return "fail";
        }
    }

    @RequestMapping(value = "/delTourist",method = RequestMethod.POST)
    public @ResponseBody String  delTourist(int order_id,String tourist_id, Model model){
        int list= orderService.delTourist(order_id,tourist_id);
        if(list>0){
            return "success";
        }else {
            return "fail";
        }
    }
}
